package com.zqx.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zqx.domain.Dorm;
import com.zqx.domain.Move;
import com.zqx.domain.Student;
import com.zqx.service.DormService;
import com.zqx.service.MoveService;
import com.zqx.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("move")
public class MoveController {
    @Autowired
    private MoveService moveService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private DormService dormService;

    @RequestMapping("listMove")
    public String listMove(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "4", required = false) Integer pageSize, Move move, Model model) {
        //分页start
        if (pageNum < 0 || pageNum.equals("") || pageNum == null) {
            pageNum = 1;
        }
        if (pageSize < 0 || pageSize.equals("") || pageSize == null) {
            pageSize = 4;
        }
        PageHelper.startPage(pageNum, pageSize);
        //分页end
        //模糊查询，搜索start
        QueryWrapper<Move> qw = new QueryWrapper<>();
        if (move.getSname() != null) {
            qw.like("sname", move.getSname());
            //当点击搜索按钮，sname将被赋予值，qw会存储后进行模糊查询
        }
        //模糊查询，搜索end
        List<Move> list = moveService.list(qw);
        PageInfo<Move> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        List<Dorm> list1 = dormService.list(null);
        model.addAttribute("dormList", list1);
        List<Student> list2 = studentService.list(null);
        model.addAttribute("studentList", list2);
        return "admin-move-list";
    }

    @RequestMapping("saveMove")
    public String saveMove(Move move, Integer did) {
        Dorm byId = dormService.getById(did);
        if (byId.getStatus() == 1) {
            return "redirect:/move/listMove";
        }
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.eq("sname", move.getSname());
        Student one = studentService.getOne(qw);
        move.setInid(did);
        move.setSimage(one.getSimage());
        move.setMtime(new Date());
        move.setStatus(0);
        Dorm byId1 = dormService.getById(did);
        move.setIndorm(byId1.getBuild() + "-" + byId1.getDormid());
        if (one.getDid() == null) {
            move.setOutdorm("暂未分配宿舍");
        } else {
            Dorm byId2 = dormService.getById(one.getDid());
            move.setOutdorm(byId2.getBuild() + "-" + byId2.getDormid());
        }
        boolean save = moveService.save(move);
        return "redirect:/move/listMove";
    }

    @RequestMapping("agree/{id}")
    public String agree(@PathVariable Integer id) {
        Move byId = moveService.getById(id);
        //状态码的修改
        byId.setStatus(1);
        moveService.updateById(byId);
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.eq("sname", byId.getSname());
        Student student = studentService.getOne(qw);
        if (student.getDid() == null) {
            Dorm byId1 = dormService.getById(byId.getInid());
            byId1.setSetting(byId1.getSetting() + 1);
            dormService.updateById(byId1);
        } else {
            Dorm byId1 = dormService.getById(byId.getInid());
            byId1.setSetting(byId1.getSetting() + 1);
            dormService.updateById(byId1);
            Dorm byId2 = dormService.getById(student.getDid());
            byId2.setSetting(byId2.getSetting() - 1);
            dormService.updateById(byId2);
        }
        student.setDid(byId.getInid());
        studentService.updateById(student);
        return "redirect:/move/listMove";
    }

    @RequestMapping("disagree/{id}")
    public String disagree(@PathVariable Integer id) {
        Move byId = moveService.getById(id);
        //状态码的修改
        byId.setStatus(2);
        moveService.updateById(byId);
        return "redirect:/move/listMove";
    }
    //删除迁移信息
    @RequestMapping("delMove/{id}")
    public String delMove(@PathVariable Integer id){
        boolean b = moveService.removeById(id);
        return "redirect:/move/listMove";
    }
    //批量删除迁移信息
    @ResponseBody
    @RequestMapping("batchDeleteMove")
    public String batchDeleteMove(String idList){
        String[] split = StrUtil.split(idList, ",");
        List<Integer> list = new ArrayList<>();
        for (String s:split
        ) {
            if (!s.isEmpty()){
                int i = Integer.parseInt(s);
                list.add(i);
            }
        }
        boolean b = moveService.removeByIds(list);
        if (b){
            return "OK";
        }else {
            return "error";
        }
    }
}
